// components/popup/payment/index.js
import User from '../../../utils/user';
import Util from '../../../utils/utils';
import { CartHttp } from '../../../api/cart.js'
import md5 from "../../../utils/md5";
const cartModel = new CartHttp();
let needPwd = false;
if(User.UserData.hasOwnProperty('common')){
  needPwd = User.UserData['common']['pass_noneed'];
}
let receiveProtocol = Util.cache_get('receiveProtocol',false);
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    showPayTime: {
      type: Boolean,
      value: false
    },
    showNeedPwd: {
      type: Boolean,
      value: true
    },
    showProtoCol: {
      type: Boolean,
      value: true
    },
    isCheckNotPwd: {
      type: Boolean,
      value: needPwd
    },
    isCheckedProtocol: {
      type: Boolean,
      value: receiveProtocol
    },
    payment: {
      type: String,
      value: '4'
    }, // 支付方式
  },
  timeTimeout:null,
  /**
   * 组件的初始数据
   */
  data: {
    pwdVal:'',
    pwdText:'',
    payFocus:false,
    userInfo:User.UserData,
    isCheckNotPwd:needPwd,
    surplusPayTimeSecond: 900,
    surplusPayTime: '15:00',
    checkedIcon: {
      active: '/images/icon/icon_check_active.png',
      normal: '/images/icon/icon_check_inactive.png',
      normalEmpty: '/images/icon/icon_check_inactive_empty.png'
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    popupPayClose: function () {
      this.setData({
        show: false,
      });
      this.triggerEvent('close')
    },
    onChangeNotPwd:function(event) {
      this.setData({ isCheckNotPwd: event.detail });
    },
    // 到店自取协议
    onChangeProtocol(event) {
      console.log(event)
      this.setData({ isCheckedProtocol: event.detail });
      Util.cache_put('receiveProtocol',event.detail);
    },
    /**
     * 密码输入获取焦点
     */
    getFocus: function (e) {
      this.setData({ payFocus: true });
    },
    /**
     * 输入密码监听
     */
    inputPwd: function (e) {
      this.setData({ pwdVal: e.detail.value });
      if (e.detail.value.length >= 6) {
        this.setData({
          payFocus: false,
        });
        this.submitPwd();
      }
    },
    submitPwd:function(){
      let self = this;
      let param;
      // if (this.data.isCheckedProtocol==false){
      //   Util.msg('请先阅读《到店自取协议》!');
      //   return false;
      // }
      if(this.data.pwdVal.length !== 6){
        Util.msg('请输入正确格式的支付密码');
      }
      let pwd = md5(this.data.pwdVal);
      param = {
        pwd: pwd.toUpperCase(),
        pass_noneed: this.data.isCheckNotPwd
      };
      if(User['UserData']['common']['pay_pass_seted'] === false){
        //主动设置密码
        Util.request({
          url:'/s/userinfo/SetPayPassword',
          data:param,
          callback:function (res) {
            cartModel.verifyPayPassword(param).then((res) => {
              self.triggerEvent('confirm', {
                pwd: pwd.toUpperCase(),
                pay_key: res.pay_key
              });
              self.setData({
                pwdVal:'',
                pwdText:'',
                show:false,
                payFocus:false,
              })
            },(res) => {
              self.setData({
                payFocus:true,
                pwdText:'',
                pwdVal:'',
              })
            });
          }
        })
      }else{
        cartModel.verifyPayPassword(param).then((res) => {
          self.triggerEvent('confirm', {
            pwd: pwd.toUpperCase(),
            pay_key: res.pay_key
          });
          self.setData({
            pwdVal:'',
            pwdText:'',
            show:false,
            payFocus:false,
          })
        },(res) => {
          self.setData({
            payFocus:true,
            pwdText:'',
            pwdVal:'',
          })
        });
      }

    },
    // 确认
    onConfirm:function() {
      this.triggerEvent('confirm', {
        payment: this.data.payment
      }, {});
    },
    countdown: function () {
      let second = this.data.surplusPayTimeSecond;
      let self   = this;
      if (Util.fixInt(second) === 0) {
        clearTimeout(self.timeTimeout);
        return;
      }
      self.timeTimeout = setTimeout(function () {
        second = second - 1;
        let l = ":";
        let s = second % 60;
        if (s < 10) {
          l += "0"
        }
        self.setData({
          surplusPayTimeSecond: second,
          surplusPayTime: Math.floor(second / 60) + l + s
        });
        self.countdown();
      }, 1000);
    },
    emptyPwd:function () {
      this.triggerEvent('emptyPwd');
      Util.toPage('/pages/me/setting/pay/index?repwd=1');
    }
  }
});
